package com.project.controller.web;


import com.alibaba.druid.util.StringUtils;
import com.project.common.utils.MyPageHelper;
import com.project.common.utils.PublicUtils;
import com.project.entity.AttendExam;
import com.project.entity.Exam;
import com.project.myBeans.AttendExamStatus;
import com.project.myBeans.ExerciseDetail;
import com.project.myBeans.R;
import com.project.service.AttendExamService;
import com.project.service.ExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@EnableAutoConfiguration
@RestController
@Api(description = "参加考试记录表",value = "attendexam")
@RequestMapping("/attendexam")
public class AttendExamController {

    @Autowired
    AttendExamService attendExamService;

    @Autowired
    ExamService examService;
    /**
     * wwh 2018/04/02
     * @param attendExam
     * @return
     */
    @PostMapping("/findByCondition")
    @ApiOperation("条件查找")
    public R findByCondtion(@RequestBody AttendExam attendExam){
        try{
            List<AttendExam> attendExamList = attendExamService.findByCondition(attendExam);
            return R.ok().put("data",attendExamList).put("total", MyPageHelper.getTotal(attendExamList));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    /**
     * Tate 2018/5/10
     * @param examid
     * @param Studentids
     * @return
     */
    @ApiOperation("批量添加考试学生")
    @PostMapping(value = "/addBatch/{examid}")
    public R addBatch(@PathVariable Integer examid,@RequestBody String Studentids){
        try{
            List<Integer> studentidList=new ArrayList<>();
            if(!StringUtils.isEmpty(Studentids)&&!studentidList.equals("N")) {
                studentidList = PublicUtils.getIntIdList(Studentids, ",");
            }
                attendExamService.addBatch(examid, studentidList);
                return R.ok("批量添加成功");
        }catch(Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * Wqd 2018/5/29
     * @param studentids
     *  @param examid
     * @return
     */
    @ApiOperation("添加学生")
    @PostMapping("/add/{examid}/{studentids}")
    public R add(@PathVariable String studentids, @PathVariable Integer examid){
        try{
            List<Integer> studentidList=new ArrayList<>();
            if(!StringUtils.isEmpty(studentids)&&!studentidList.equals("N")) {
                studentidList = PublicUtils.getIntIdList(studentids, ",");
            }

             for(Integer studentid :studentidList){
                 AttendExam attendExam=new AttendExam();
                 attendExam.setExamid(examid);
                 attendExam.setStudentid(studentid);
                 System.out.println(attendExamService.findByCondition(attendExam).toArray().length);
                 if(attendExamService.findByCondition(attendExam).isEmpty()){
                     attendExamService.add(attendExam);
                 }
             }
            return R.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * @Description:
     * @param: timestamp
     * @return: boolean
     * @author: TateBrown
     * @Date: 2018/7/3
     */
    @ApiOperation("返回考试是否结束的标志")
    @PostMapping("/isend/{examid}")
    public R isend(@PathVariable Integer examid) {
        try {
            Exam exam = examService.findById(examid);
            Timestamp finish = exam.getFinishtime();
            Timestamp curtime = new Timestamp(System.currentTimeMillis());
            if (curtime.before(finish)) {
                return R.error().put("data", curtime);
            } else {
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
    /**
     * @author: DragonDoor
     * @Date: 2018/11/5 10:28
     * @Description:
     */
//    @ApiOperation("考试中状态监测")
//    @PostMapping("/inExam")
//    public R inExam(@RequestBody AttendExam attendExam, HttpServletRequest request)
//    {
//        int examId = attendExam.getExamid();
//        int studentId = attendExam.getStudentid();
//        Timestamp curtime = new Timestamp(System.currentTimeMillis());
//        HttpSession session = request.getSession();
//        @SuppressWarnings("unchecked")
//        Map<Integer, AttendExamStatus> map = (Map<Integer, AttendExamStatus>) session.getAttribute(Integer.toString(examId));
//        Timestamp lasttime;
//        int remaintime = 0;
//        if(map!=null)
//        {
//            AttendExamStatus curstatus = map.get(studentId);
//            lasttime = curstatus.getLasttime();
//            remaintime = curstatus.getDuration();
//            if(lasttime!=null&&(curtime.getTime()-lasttime.getTime()<5000))
//            {
//                remaintime -= (curtime.getTime()-lasttime.getTime());
//                lasttime = curtime;
//
//            }
//        }
//
//    }
    /**
     * @author: DragonDoor
     * @Date: 2018/11/5 10:27
     * @Description:
     */
    @ApiOperation("试卷提交")
    @PostMapping("/submitExam/{studentid}/{examid}")
    public R submitExam (@PathVariable Integer studentid, @PathVariable Integer examid, @RequestBody List<ExerciseDetail> exerciseDetails)
    {
        try
        {
            AttendExam attendExam = new AttendExam();
            attendExam.setExamid(examid);
            attendExam.setStudentid(studentid);
            attendExamService.submitExam(attendExam,exerciseDetails);
            return R.ok();
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }
    /**
     * @author: DragonDoor
     * @Date: 2018/11/5 10:27
     * @Description:
     */
    @ApiOperation("开始考试")
    @PostMapping("/beginExam/{studentid}/{examid}")
    public R beginExam(@PathVariable Integer studentid,@PathVariable Integer examid)
    {
        try
        {
            AttendExam attendExam = new AttendExam();
            attendExam.setStudentid(studentid);
            attendExam.setExamid(examid);
            AttendExam attendExam1 = attendExamService.beginExam(attendExam);
            Timestamp curtime = new Timestamp(System.currentTimeMillis());
            return R.ok().put("data",attendExam1).put("curtime",curtime);
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }
    /**
     * @author: DragonDoor
     * @Date: 2018/11/5 10:28
     * @Description: 
     */
//    @ApiOperation("考试中")
//    @PostMapping("/inExam/{studentid}/{examid}")
//    public R inExam(@PathVariable Integer studentid,@PathVariable Integer examid)
//    {
//        return R.ok();
//    }

}
